import 'package:drive/drive.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../res.dart';

class LineWidget extends Container {
  final Color color;
  final double height;
  LineWidget({Key key,this.color=resLineColor,this.height=1})
      : super(
    key: key,
          color: color,
          height:height,
        );
}

/// 绘制三角形

class TrianglePainter extends CustomPainter {
  Color color; //填充颜色
  Paint _paint; //画笔
  Path _path; //绘制路径
  double angle; //角度

  TrianglePainter(this.color) {
    _paint = Paint()
      ..strokeWidth = 1.0 //线宽
      ..color = color
      ..isAntiAlias = true;
    _path = Path();
  }

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final baseX = size.width * 0.5;
    final baseY = size.height * 0.5;
    //起点
    _path.moveTo(baseX, 0);
    _path.lineTo(size.width, size.height);
    _path.lineTo(0, size.height);
    canvas.drawPath(_path, _paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class AnimatedTransform extends StatefulWidget {
  final Widget child;
  final Duration duration;
  final Curve curve;
  final Offset offset;

  AnimatedTransform(
      {this.child, this.offset = Offset.zero, this.duration, this.curve});

  @override
  _AnimatedTransformState createState() => _AnimatedTransformState();
}

class _AnimatedTransformState extends State<AnimatedTransform>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Offset offset;

  @override
  void initState() {
    super.initState();
    offset = widget.offset;
    _controller = AnimationController(upperBound: double.infinity,value: offset.dy,vsync: this);
    _controller.addListener(onAnimListener);
  }

  @override
  void didUpdateWidget(covariant AnimatedTransform oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(oldWidget.offset.dy==widget.offset.dy){
      return;
    }
    _controller.stop();
    _controller.animateTo(widget.offset.dy,duration:widget.duration,curve:widget.curve);
  }

  void onAnimListener() {
    offset=Offset(0,_controller.value);
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: offset,
      child: widget.child,
    );
  }
}



class CachedImageShadow extends StatelessWidget {
  final String url;
  final double width;
  final double height;
  final List<BoxShadow> boxShadow;
  final BoxBorder border;
  const CachedImageShadow({Key key,this.url,this.border,this.width,this.height,this.boxShadow}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return    Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
          boxShadow: const [
            BoxShadow(
                color: Color(0x22333333),
                offset: Offset(0, 3),
                blurRadius: 5)
          ],
          border: border,
          image: DecorationImage(
              image: CachedNetworkImage(url, scale: 1),
              fit: BoxFit.cover),
          borderRadius: BorderRadius.circular(width)),
    );
  }
}



class NumWidget extends StatefulWidget {
  final isEnable;
  final int minNum;
  final int maxNum;
  final int num;
  final Function(int) onListener;

  NumWidget(
      {this.num = 0,
        this.isEnable = true,
        this.minNum = 0,
        this.maxNum = 999999999,
        this.onListener});

  @override
  _NumWidgetState createState() => _NumWidgetState();
}

class _NumWidgetState extends State<NumWidget> {
  int num = 0;
  final TextEditingController controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    num = widget.num;
    controller.text = num.toString();
  }

  @override
  void didUpdateWidget(covariant NumWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // print("numDidUpdate${widget.num},${oldWidget.num},${}");
    if (oldWidget.num == widget.num || widget.num == num) {
      return;
    }

    num = widget.num;
    controller.text = num.toString();
    setState(() {});
  }

  void onAddNumClick() {
    num++;
    toListener();
    controller.text = num.toString();
  }

  void onCutNumClick() {
    num--;
    toListener();
    controller.text = num.toString();
  }

  void onTextChangeListener(String text) {
    num = int.parse(text);
    if (num < widget.minNum) {
      num = widget.maxNum;
      controller.text = num.toString();
    } else if (num > widget.maxNum) {
      num = widget.maxNum;
      controller.text = num.toString();
    }
    toListener();
  }

  void toListener() {
    if (widget.onListener != null) {
      widget.onListener(num);
    }
  }

  @override
  Widget build(BuildContext context) {
    final minEnable = widget.minNum <= num && widget.isEnable;
    final maxEnable = widget.maxNum >= num && widget.isEnable;
    final minTextColor = minEnable ? Colors.black : Color(0xffC2C2C2);
    final maxTextColor = maxEnable ? Colors.black : Color(0xffC2C2C2);
    final editorColor = widget.isEnable ? Color(0xff302F2F) : Color(0xffC2C2C2);

    return Container(
      width: 80.dp,
      height: 24.dp,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.dp),
          border: Border.all(color: Color(0xffe2e2e2))),
      child: Row(
        children: [
          GestureDetector(
            onTap: minEnable ? onCutNumClick : null,
            child: Container(
              width: 24.dp,
              height: 24.dp,
              decoration: BoxDecoration(
                  color: Color(0xffF6F6F8),
                  borderRadius:
                  resBorderRadius(bottomLeft: 4.dp, topLeft: 4.dp)),
              alignment: Alignment.center,
              child: Container(
                height: 1.dp,
                width: 8.dp,
                decoration: BoxDecoration(
                    color: minTextColor,
                    borderRadius: BorderRadius.circular(1.dp)),
              ),
            ),
          ),
          Container(
            height: 24.dp,
            width: 1.dp,
            color: Color(0xffe2e2e2),
          ),
          Expanded(
              child: CupertinoTextField(
                decoration: BoxDecoration(color: Colors.white),
                padding: EdgeInsets.only(),
                keyboardType: TextInputType.number,
                controller: controller,
                readOnly: !widget.isEnable,
                textAlignVertical: TextAlignVertical.center,
                textAlign: TextAlign.center,
                style: resTextStyle.copyWith(
                    color: editorColor, fontSize: 12.sp, height: 1.4),
              )),
          Container(
            height: 24.dp,
            width: 1.dp,
            color: Color(0xffe2e2e2),
          ),
          GestureDetector(
            onTap: maxEnable ? onAddNumClick : null,
            child: Container(
              width: 24.dp,
              height: 24.dp,
              decoration: BoxDecoration(
                  color: Color(0xffF6F6F8),
                  borderRadius:
                  resBorderRadius(topRight: 4.dp, bottomRight: 4.dp)),
              alignment: Alignment.center,
              child: Text(
                "+",
                style:
                resTextStyle.copyWith(color: maxTextColor, fontSize: 15.sp),
              ),
            ),
          )
        ],
      ),
    );
  }
}


